<?php
declare (strict_types = 1);

namespace app;

use think\facade\Validate;
use think\Service;

/**
 * 应用服务类
 */
class AppService extends Service
{
    public function register()
    {
        // 服务注册
    }

    public function boot()
    {
        // 自定义exists验证规则
        Validate::maker(function ($validate) {
            $validate->extend('exists', function ($value, $rule, $data, $field, $desc) {
                $model = '\app\model\\' . parse_name($rule, 1);
                $has = $model::where($field, $value)->count();
                return $has == 0 ? false : true;
            }, ':attribute不存在!');
        });
    }
}
